import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapTest {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<Integer, String>();
        method( map);//学号和姓名
    }

    public static void method(Map<Integer,String> map){
        //添加元素
        System.out.println(map.put(1,"hello"));
        System.out.println(map.put(1,"world"));//存相同键，值会覆盖
        map.put(2,"world");
        map.put(3,"vegeta");
        map.put(4,"chenzi");

        //打印集合
        System.out.println(map);

        //删除
        System.out.println(map.remove(2));

        //判断
        System.out.println(map.containsKey(1));

        //获取
        System.out.println(map.get(1));
        System.out.println(map.size());
        System.out.println(map.keySet());

        //遍历
//        Set<Integer> keySet = map.keySet();
//        Iterator<Integer> iterator = keySet.iterator();
//        while (iterator.hasNext()){
//            Integer key = iterator.next();
//            System.out.println(map.get(key));
//        }

        //map转set就可以迭代
        Set<Map.Entry<Integer,String>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer,String>> it2 = entrySet.iterator();
        while(it2.hasNext()){
            Map.Entry<Integer,String> me = it2.next();
            Integer key = me.getKey();
            String value = me.getValue();
            System.out.println(key+":"+value);
        }
    }
}
